#!/usr/local/bin/python
# -*-encoding:utf-8 -*-
import argparse

def process(rawlog, operlog):
    rawlogFile = open(rawlog, 'r');
    operlogFile = open(operlog, 'w');
    for line in rawlogFile:
        li = line.strip().split('\t');
        if len(li) != 6:
            print 'Wrong';
            continue;
        queryString = li[0];
        appID = li[2];
        clickCount = int(li[4]) + int(li[5]);
        operlogFile.write('%s\t%d\t%s\n' % (appID, clickCount, queryString));
    rawlogFile.close();
    operlogFile.close();

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Filtering operation log from the format given by Xin Jiang');
    parser.add_argument('rawlog', help='rawlong input');
    parser.add_argument('operlog', help= 'format log output');
    args = parser.parse_args();

    rawlog = args.rawlog;
    operlog = args.operlog;

    process(rawlog, operlog);
